Hi everybody. This is our video lecture for Week 13. We are going to continue learning about linear regression today. And this will be part of your final projects. So do make sure you review this and if you have any questions, you can always let me know or stop by office hours. But yeah, here we go. So, so far we have covered things in class like the one proportion z-test and the two proportion z-test, one-sample t, 2 Sample t, and a simple linear regression. That's what we've got so far. This week. We're going to cover some basic multiple linear regression. Okay, we are going to talk about, first of all, building regression models. These are categorical predictors for just something that you might be interested in doing. We're going to combine the categorical and numerical information. We'll talk about how to check assumptions for a multiple linear regression, which we covered simple linear regression last week. So hopefully this is more or less straight forward. And then we're going to talk about something called R squared and adjusted R-square. So, whoops, there we go. So like I said, we did a simple linear regression altering weeks files. And it's for simple linear regression. We were working on PK, two numerical variables, failure or comparing a response versus a numerical picture. Pay is. Hopefully you can think of the scenario where we might want to predict something based on a categorical predictor. Okay? To do that, we're going to use something called an indicator variable, which is a special type of categorical variable that's been coded as a 0 or pay using an indicator variable and placing the category me and allows for barriers to views directly. It doesn't make sense to think of something being applied to multiple linear regression. It's like a weekday versus what we get when I plug in the word saturday into an equation. But you could put in zeros and ones. And that's why we use indicator variables. Okay, so we're actually going to use that example that I just talked about. So continuing using the rail trail data set, we know that it has a bunch of whether two variables, so high temperature, low temperature, etc. We're going to look specifically at this, this last these last two actually. So we've got weekday and daytime. We're going to look at, I think weekday, which is coded as false if it's not a weekday. So 0 if you're recording the information on Tuesday, Wednesday, Thursday, Friday. And true sorry, I got that backwards or false. If it's not a weekday, which will be Saturday and Sunday. And true if it is a week, Monday through Friday. Hey, remember in R that falses are equivalent to zeros and choose RPA to y. So it turns out that actually we can think of categorical variables will be all sorts of different things. Either did a variable name like weekday or weekend, or it could be a false and true, or it could be a 0 again. Well, so here we go. Here's the first few values in the Weekday variable. You can see it. False, true, true. So this is from Tinder. Sorry, there sometime during the week, right? This one is during the weekends. During the week. And it turns out that I could actually just put that directly into R the same way I would say a numerical predictor, right? So I'm using the lm command for linear model. I have my volume being my response variable modeled by or predicted by this. It had told them that I got weekday and it's going to give me out a intersect just like it, whatever the numeric or predictor at a slope, just like you would with a numerical predictor. So we notice that it has an intercept and a slope just like a numeric variable. But what changes finally are the interpretations of the crackers. So now we're back to having an inducement that does make sense. Basically, you only have zeros and ones. 600 is one of the x values that you're using to fit your model. And so we're not extrapolating if we interpret the intercepts, the integrant, sorry, the intercepts can be interpreted as the value of the response variable for the first category. Which is when the indicator is equal to 0 and is sometimes known as the baseline for reference flow. Okay, So we said here that it was 0 when it's a weekend. So basically the intercept is 430.71. We extract 4371 railsvm your users during a week. Okay, The Slow also changes because the slope previously was known as the 2020 upon a one unit increase in x. Now all you have a one unit increase, right? You're changing from 0 to one. So turns out that the slope actually is the average change in the response variable between the two categories. So the slope is 8.29, which means that we should expect negative 8.29, which means that we should expect inadequate 29 fewer rail trillions or strain weekdays, which makes sense people have less free time in general on the weekdays than they do on the weekends. So we can say that overall we expect 30, 150 point 4 2, which is equal to 430.71 minus 8.293150 ish baritone on weekdays. So we've got some new kind of interpretations for these two mountains. Okay? That was how to interpret, sorry, include an indicator, represent Jian categorical variables with two categories. But we also might be interested in including a categorical variable with three categories or three levels. Okay, Can we do it? Can we incorporate these things with more than two levels? Maybe we want to protect the volume of people on the rail trail by a season, right? We know we have observations for a spring, summer, and fall. So we can't plug spring into a model. We can't put the words into a model that doesn't make sense. So what should we do? Give we coded before as zeros and ones? It might be reasonable, you might think to code things as zeros and ones and choose. So let's look at what, at what happens if we do that. You can see that it would look like 1, 1, 0, 1, 0, 0, 0, 1 I choose. We haven't seen any two observations yet. And so we will look at our intercept and our season slope. So the model would be 384.4 ish plus negative 16.52 times season. So when we have a one unit increase in season, let's say we go from spring two, SAR, we would expect negative 16.52 people to be writing on a rail trail. And then we have another one unit increase, let's say from glad to summer to fall, we would expect it to decrease again. However, that doesn't really make sense because basically what you are doing is assuming that the difference between spring and summer is the same as the difference between summer and fall, right? One minus 0 is 12, minus one is one. But the difference between those two things isn't necessarily the same. We don't want to assume the same, but they're saying because we don't know for sure. So we can actually pursue this strategy. There we go. Here's what I just said, right? Every time you see some changes, which is the same thing in this dataset has added. And what we would expect the number of writers to decrease by about 16.5. The increase is not really significant. But also more importantly, it has a really nice sense. Here's what we want to do instead. When we fit a regression model with a categorical variable that has K levels. And anytime k is greater than two, you want to incorporate a different coefficient for k minus one of those slots. The last syllable that doesn't receive the coefficient is known as the reference level. So the reference level, that's what happens. But of all of your indicators are 0. Hey, that means that all of the other levels, all of the coefficients for everything else are going to be considered relative to the reference level. And I am an example down here. So in this example, all is the reference up, right? Fall is what happens when it's not spring, summer. Remember that we don't count data for winter break, so only thing we have a spring, summer. So we've included an indicator variable here for spring and an indicator variable here for summer. We've got three levels, spring, summer, and fall, but we only have two coefficients in R. Hey, so here you can look at this is actually what happens when I include sees, it, sees an a somewhere. So our spring, summer, spring, spring, it's got three levels here, fall, spring and summer and fall. The baseline. You can see that I fit a model here and are automatically knows that because it's a factor, because it's categorical. It needs to fit a different coefficient for spring and a different coefficient for sine k. I've already said that fall is the reference level for season. And you know that because Paul isn't included in the table here, because false not included. It must be the reference. Okay, now let me go ahead and look at the coefficients in our table. So first of all, the intercept is going to be what we expect in the fall. So basically this is saying that in the fall we expect an average of 295 users on the real true. Okay. In the spring here, we know that this is going to represent the change between spray Taxol. So on average, we expect 76 more writers on the spring on the rail trail in the spring than we had in the fall. Okay. Same thing for summer. In the summer, that coefficient is a 126.8. So we expect a 126.8 more writers In summer or users in the summer than we do. Again, relative to the fall, rate falls the reference level. So it's always relative to the poll. Okay? Here's just the same thing. Reference always fall because there's nothing there. And the expected number of users in the spring is 70.76.6 more than the fall. And the second number is used. It has summer is 127 points or sorry, 126. 127. I didn't say this before, but we can also check out the significance of these. So we can know the season is a significant predictor. For summer is very small, it's white 000 4. So knowing those summaries, significant predictor, the p-value for fall versus spring is very nearly significant. You, you wouldn't technically reject this null hypothesis, but it insignificant. And since they come as part of a package deal, you can't separate out spring, summer? I would probably be both. Okay. So that's how we would include a categorical predictor. So now you can put together regression based on numerical predictor. If you put together our regression based on a categorical predictor. And you might say to yourself, well, what if I want to use both types? It turns out that we can expand from simple linear regression to using something called multiple linear regression. Multiple linear regression, also sometimes known as multiple regression, models, a response variable to spawn response based on more than one explanatory variable. Okay? You can related predictors, like in this scenario, you've actually already seen multiple regression because they included two terms. Or they can be different, right? You could start to put in things like Aboriginal culture and weekday, which might give you more information than either one of those variables on your own. Okay? You can see here that we've got lots of parameters, right? There's a, b beta 0, beta one and so on. I can't give you equations for these, not like in simple linear regression. This is a time where we're just going to go ahead and use statistical software to do overestimation here. As I said, we can use multiple different types of information. They could be categorical and numerical. You can mix different types. And it turns out, hopefully, hopefully for all of you, that we can pretty easily extend everything we know about simple linear regression, multiple linear regression. Okay? We can still do things like predict the volume of writers on a given day. We could still do things like calculate residuals. We can sell timber parameters, and we can still perform in France, right? We can still look at confidence intervals and hypothesis tests. Okay? It turns out that it's actually really easy to fit the models were suddenly we use the same lm command. We're still going to use a formula, but now we're literally just adding the additional barrier. So previously we had modeled by high time, now we have volume model by high-tech AS weekday. And you can look at, when we look at the summary, you have now a role for the intersect URL for the slope on high temperature and a row for this indicator variable for weekday. Both the p-values for these two variables in the model are small, right? This one's quite 000, 000, 000, 000, 001. Very tiny. This one's point 0, 3, which is not asked, Hey, it's so significant. So based on this, I would say that but I can sure. Yeah, it's been a day being a weekend or not is significantly affecting the number of rail trail users. Hey, most of our inference. So we do know though that when you have a lot of time checking the assumptions and conditions. So let's go ahead and look at those. Last week we learned about plots. I'm going to talk about those Seahawks on, which are sometimes known as diagnostic plots. We know from simple linear regression that you have to check residuals to see that there's linearity presents that they have to be in your normal. We have to separate a constant variability. Independence initiates out that for the most part we're going to check those exact same things at multiple layer. Gosh, not exactly the same way, but the same types of assumptions. Okay, in simple linear regression, it turned out that we just check the residuals plotted against the predicted values to look for patterns, which we can do here again. And it turns out that if there's more than one x-value, we can actually still look at the predicted values because the predicted values take into account all of the x-values. The other thing that you can do is check the residual plots for each predictor. So instead of having predicted values on the x axis, you would have the actual x-values and we'll look at that in a second. But we're going to use these different types of plots for two things at a time. Number one, we're going to check linearity. So I, I think with a curved pattern president that we saw last week. So I'm going to be careful about this in my overall predictions, but I'm also going to look for constant area here. Gary ability, I don't see a triangle shaped, cavernous fossa where at least good with respect to that assumption. Right here is the residual plot for each of the x values. And like I said, we can still check out the high temperature here. I am a little concerned because they're so what do you call occur? I'm also looking out the weekday. So on the left-hand side here we have gas and here we have weekdays. It's a little early 2000s scatter plot for two columns. Basically. I'm not seeing a pattern there started uniformly distributed across the line equals 0. What I am seeing actually with me a little bit confusing. There is sort of a wedge shape, right? The variability isn't constant. There's much bigger spread on false column and there is other true com isn't quite big enough to give me like too much concerned, but you can see why that might be an issue. And that's the example of seeing this pattern actual, real residual plot. Okay. Have it will, excuse me, having reviewed these diagnostic plots are sort of two options. So in both of the plots and all the pots, actually we sorry, issues. If we're not terribly, terribly concerned about any issues, we can use this as our right. We, we know that we can check all the assumptions. We know that we can use it. We feel okay, right? We clean it also, if we were more concerned that the r, We could try and improve the mouth that by using more complicated methods that can include things like fitting curves to our data and some other types of issues. These would be more covered and start to 42 for what you can do right now, just know that you sort of future limits. But if you continue to type classes, you'll learn more about that. Hey, we haven't seen so far something. It's our horribly, horribly violated, but do keep an eye out, right? That that can be a problem. So they're very POP violated. If you've got something that's like very, very, very bad, either simple or multiple regression. You might want to consider a completely different model using techniques that we've not learned in this class, right? So if, if you're for some reason and stopping at this statistics class, which is fine. But you get to a scenario later we're seeing something where it's really notable rates due to simple linear regression. Just stop and report that you don't have the tools to fix it, right? Don't try and model if the assumptions are really horribly Bye. Okay. So that's that. Now I think a reasonable question is, we've got basically three different models, right? We've got a model predicting the volume of users from, huh? Yes, high temperature alone. We've got a model predicting users from weekday alone. And now we have a model predicting what do you call it, a volume of users from both of the button. And so it's reasonable for you, I want to say, well, which one should we use? How do we know? And the way that we can get the best model is by talking about something called r squared. Okay? You actually already know what r by itself is. So RNA itself is correlation, which we covered several weeks ago. But correlation is one way of evaluating strength of a linear relationship between two variables. Okay? What we're going to do in this class is instead switch to something called r squared, which is literally just the correlation squared. Okay? It turns out that R-squared for linear model describes the amount or proportion of variation in the response variable that can be explained by the least squares line. Alright? And there's only one predictor in the model saying that the variation can be explained by the predictor is the same as saying is that the variation can be explained by the least square science. So those two things are the same if you see that, don't get yourself confused. But for example, what does this look like in practice? Well, we know that the correlation between high temperature at volume is 0.583. So we know that 33.9% or variation of the estimated number of rail trail users can be explained by the day's high temperature. Or we would also say 33.9% of variation in the rail trail users can be explained by the least squares line. Either of these is fine. Okay, things that are useful to know about R squared. R we know is negative one to positive one. So R-squared is always between 01. And in general, we're looking for a high number, right? You want to explain a lot of this variation. So we want to make a model with a high R-squared. Okay? Here we go. When there are multiple predictors in the model saying the variation can be explained by the least squares line is the same as saying the variation that can be explained by all of the predictors together. So, so that's what we have and we can compare different models using R squared. And as I just said, we want to look for the high R-squared. So I have high attempt by itself has an R-squared 33.9 and weekday by itself is 0.086, which is pretty low. Then I have both the model one which I think is high-tech, sweet a scanner, has an R-squared of 37.35. So this one is the highest. And we would want to use both high temperature and weekday to get. Okay. So I think the general idea, but it is helpful to know it was something that we have to consider. Is that r-squared my uncles except for when there's just a single variable, but is less helpful when there are many. So we can start to think about adding extra things to our model. We know we have things like precipitation, we know we have, it's like how cover. And I think it makes sense why the number of users might change if it's raining, super cloud, right? But eventually we would run out of these reasonable variables and we would start adding irrelevant ones. So such as the score of the Red Sox game tonight before. I don't know why that might impact it, but it might. But it turns out if you're having unreasonable seconds, you're always going to get a higher R squared. It turns out that that's just a mathematical fact that you add extra stuff, even if it doesn't help the model at all. Your R-squared is always by Jane piece and that's not good. So what we're going to do instead is consider something called adjusted R-squared. Okay? We can re-express R-squared instead of being installation of the received, sorry, the correlation, which variable squared? We can re-express it as one minus the variability of the residuals or with a very naughty outcome. And then it turns out if we make some very small adjustments, we get this r squared formula, which is slightly different. But basically what's important is this over here. R-squared you can see adjusts for k, which is the number of predictors in your models. So every time you add a variable, that's going to penalize you slightly for adding something. And you're kind of hoping that the game it explained variability makes up for this extra penalty. Okay? It turns out that it's actually really easy to extract adjusted R-squared from all your weights to the thyroid function at a dollar sign d j dot dot squared for our adjusted R squared. I'm here looking at volume by itself and that bond plus weekday. And it turns out that on my adjusted R squared for model 1, which has both weekday and high-temperature, is 0.359 relative to point 3, 3, 2. So this is still higher, it's still higher to consider retail as well as high temperature. Okay, even after adjusting for my extra predictor, the bigger house seems to be better. It's worth winning out. Another thing here, the adjusted R-squared, because it's tantalizing for predictor variables. It's always going to be slightly smaller than R squared for the same model, which is five. That's just because you're getting your penalty. You still want to use adjusted R-square to compare multiple models. Okay? So that's how you would end up comparing. You do want to make a final selection, and I do want to give you a couple of sort of warnings about model selection. So keep in mind, first of all, device model is not always the most complicated. You want to try and keep things as simple. And if possible. Sometimes, including variables that are not super important can actually reduce accuracy of predictions, right? So we want to get rid of those variables that aren't important. We also because we want to keep it as interpretable at speak. Yeah. Okay. On that means we can use the model selection strategies, including comparing adjusted R-squared rate. You don't want to ignore those. You want to include the penalty for the extra variables on that can help you get rid of models that are, sorry, variables from your model that are less important. And where did he come up with a simple model? It's really common to refer to 200 miles. It's parsimonious. You want as a statistician, as parsimonious model, if you want something that explains the data well, it uses the fewest terms possible. Okay, the other thing that I want to end with Inspire, please, please, please be careful not to make any causal interpretations using multiple regression or simple or your crushed for that. Most multiple linear regression, simple linear regression are based on observational data. And if you remember all the way from the very first week of class, we can't make a causal inference if we only have observational study data. You want to be very careful about implying a causal relationship from something that's only correlate. Hey, that doesn't mean that the models are useless, right? Just because we can't make the causal connection. It's, it's not like it's not important. These models are also the first step. But just try and avoid the temptation as much as you can, because it can be really tough to write, especially here. So only make a causal interpretation or causal connection. You can only do it if you're not experimental data. Not only if you have observational study data, but remember it takes a lot to say that something causes the other. So just be very careful. Hey, that's the last slide, I'm not. So you've actually seen all of the material that we were going to do for this class. Congratulations, you've made it through. Of course, we'll come up with will do some review next week and we'll see some more eye on your homework and out in your final project. So you can always work on this a little bit more or ask me questions, but yes, I think give yourself a pat on the back. Well done. And I will see everybody when you return to the grid.